package top.vanchor.flexible.utils;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 98523
 * @create 2024/4/7 19:39
 */
import org.springframework.stereotype.Component;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Component
public class MD5Encoder {

    public static String encode(String input) {
        try {
            // 创建MD5加密算法实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 对输入字符串进行加密
            byte[] bytes = md.digest(input.getBytes());
            // 将加密后的字节数组转换为十六进制字符串
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                result.append(String.format("%02x", b));
            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            // 处理加密算法不支持的异常
            e.printStackTrace();
            return null;
        }
    }
}
